<template>
  <div class="detail-page">
    <van-nav-bar
      left-text="返回"
      @click-left="$router.back()"
      fixed
      :title="'未知标题'"
    />
    <header class="header">
      <h1>{{ detailData.stem }}</h1>
      <p>
        {{ detailData.createdAt }} | {{ detailData.views }} 浏览量 |
        {{ detailData.likeCount }} 点赞数
      </p>
      <p>
        <img :src="detailData.avatar" alt="" />
        <span>{{ detailData.creator }}</span>
      </p>
    </header>
    <main class="body" v-html="detailData.content" />
    <div class="opt">
      <van-icon
        :class="{ active: likeStatus }"
        @click="likeCollect(1)"
        name="like-o"
      />
      <van-icon
        :class="{ active: collectStatus }"
        @click="likeCollect(2)"
        name="star-o"
      />
    </div>
  </div>
</template>


<script>
import { articleDetail_API, LikeCollect_API } from "@/api/article.js";
export default {
  data() {
    return {
      id: this.$route.params.id,
      detailData: {},
      likeStatus: false,
      collectStatus: false,
    };
  },

  async created() {
    await this.getDetail();
    this.likeStatus = this.detailData.likeFlag;
    this.collectStatus = this.detailData.collectFlag;
  },

  methods: {
    // 获取详情
    async getDetail() {
      const { data } = await articleDetail_API(this.id);
      this.detailData = data.data;
    },
    // 点赞 & 收藏
    async likeCollect(num) {
      await LikeCollect_API(this.detailData.id, num);
      if (num === 1) {
        this.likeStatus = !this.likeStatus;
        this.$toast.success(this.likeStatus ? "取消点赞" : "点赞成功");
        this.likeStatus
          ? this.detailData.likeCount++
          : this.detailData.likeCount--;
        return;
      }
      this.collectStatus = !this.collectStatus;
      this.$toast.success(this.collectStatus ? "取消收藏" : "收藏成功");
    },
  },
};
</script>

<style lang="less" scoped>
.detail-page {
  margin-top: 44px;
  overflow: hidden;
  padding: 0 15px;
  .header {
    h1 {
      font-size: 24px;
    }
    p {
      color: #999;
      font-size: 12px;
      display: flex;
      align-items: center;
    }
    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
    }
  }
  .opt {
    position: fixed;
    bottom: 100px;
    right: 0;
    > .van-icon {
      margin-right: 20px;
      background: #fff;
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 50%;
      box-shadow: 2px 2px 10px #ccc;
      font-size: 18px;
      &.active {
        background: #fec635;
        color: #fff;
      }
    }
  }
}
</style>